namespace Roids.SL
{
    using System.Collections.Generic;
    using Framework;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;

    public class RoidsGame : Game
    {
        private readonly IList<ISceneComponent> _components = new List<ISceneComponent>();
        private AsteroidSet _asteroids;
        private Texture2D _background;
        private SpaceShipBlasts _blasts;

        private SpriteFont _font;
        private SpaceShip _spaceShip;
        private SpriteBatch _spriteBatch;

        public RoidsGame()
        {
            new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            _blasts = new SpaceShipBlasts(this);

            _spaceShip = new SpaceShip(this, _blasts);
            _spaceShip.CenterIn(Window.ClientBounds);

            _asteroids = new AsteroidSet(this);

            _components.Add(_asteroids);
            _components.Add(_blasts);
            _components.Add(_spaceShip);

            _asteroids.StartLevel();

            Components.Add(new TitleOverlay(this));

            base.Initialize();
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(GraphicsDevice);
            _background = Content.Load<Texture2D>(@"Sprites\bg");
            _font = Content.Load<SpriteFont>(@"mainFont");

            foreach (var item in _components)
            {
                item.LoadContent();
            }

            base.LoadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            var gs = GamePad.GetState(PlayerIndex.One);

            // Allows the game to exit
            if (gs.Buttons.Back == ButtonState.Pressed)
                Exit();

            foreach (var asteroid in _asteroids.Items)
            {
                var shipCollision = asteroid.CheckCollision(_spaceShip);
                if (shipCollision.HasCollided)
                {
                    AsteroidHitPlayer(asteroid, shipCollision);
                }

                _blasts.CheckCollision(asteroid, AsteroidShotByPlayer);

                foreach (var asteroid1 in _asteroids.Items)
                {
                    if (asteroid1 == asteroid) continue;
                    if (asteroid.NoCollisionLife + asteroid1.NoCollisionLife > 0) continue;
                }
            }

            foreach (var item in _components)
            {
                item.Update(gameTime);
            }

            CleanUpDeadItems();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            var rect = new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height);

            _spriteBatch.Begin();
            _spriteBatch.Draw(_background, rect, Color.White);

            foreach (var item in _components)
            {
                item.Draw(gameTime, _spriteBatch);
            }

            _spriteBatch.End();

            base.Draw(gameTime);
        }

        private void AsteroidShotByPlayer(Blast blast, Asteroid asteroid, CollisionResult collision)
        {
            blast.ReadyToRemove = true;
            asteroid.ReadyToRemove = true;

            //CreateAsteroidFragments(asteroid, fragments, collision);

            //_scoreCounter.AddPoints(asteroid.Size * 100);

            //var explosion = new AsteroidExplosion { X = collision.CollisionPoint.X, Y = collision.CollisionPoint.Y };
            //Add(explosion);
        }

        private void AsteroidHitPlayer(Asteroid asteroid, CollisionResult shipCollision)
        {
            asteroid.ReadyToRemove = true;

            //var explosion = new AsteroidExplosion { X = ship_collision.CollisionPoint.X, Y = ship_collision.CollisionPoint.Y };
            //Add(explosion);
        }

        private void CleanUpDeadItems()
        {
            _asteroids.Clean();
            _blasts.Clean();
        }
    }
}